/*
 * CEnvironment.cpp
 *
 *  Created on: 11 Oct 2012
 *      Author: Mario Kleinsasser
 *		Time:	19:05:35
 */

#include "CEnvironment.h"
#include "CAnthill.h"
#include "CFood.h"
#include "CItemFactory.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "CEmpty.h"
#include <typeinfo>

using namespace std;

/* IMPORTANT set the instance pointer to 0 (NULL) on init*/
CEnvironment* CEnvironment::instance = NULL;

CEnvironment &CEnvironment::getInstance() {
	if (!instance) {
		/* if no CEnvironment instance exists, create the one and only */
		instance = new CEnvironment();
	}

	/* return the existing object */
	return *instance;
}

CEnvironment &CEnvironment::getInstance(int M, int N) {
	if (!instance) {
		/* if no CEnvironment instance exists, create the one and only */
		instance = new CEnvironment(M, N);
	}

	/* return the existing object */
	return *instance;
}


void CEnvironment::destroy() {

	//TODO: Delete the <list> of areas

	/* delete the object */
	delete instance;

	/* set back instance count */
	instance = NULL;
}

void CEnvironment::printAll() {

	/*
	 * Print the environment and statistics
	 */
	CEnvironment &environment = CEnvironment::getInstance();
	CArea* row = environment.getPlayground()->getFirst();
	CArea* col = environment.getPlayground()->getFirst();

	int anthillFood = 0;
	int anthillBornAnts = 0;
	int anthillDiedAnts = 0;
	int anthillBackAnts = 0;

	while (row != NULL) {
	    while (col != NULL) {
			list<CItem*> ltmp = col->getItemList();
			list<CItem*>::iterator it;
			if (ltmp.size() <= 1) {
				it = ltmp.begin();
				(*it)->print();

			} else {

				for (it = ltmp.begin(); it != ltmp.end(); it++) {
					/*
					 * try catch block to get statistics
					 */
					try {
						if (((*it)->getClassType()) == "anthill"){
							CAnthill *anthill = dynamic_cast<CAnthill*>(*it);
							anthillFood = anthill->getFood();
							anthillBornAnts = anthill->getAntcount();
							anthillDiedAnts = anthill->getAntdiecount();

							list<CItem*> backAnts = anthill->getAntList();
							anthillBackAnts = backAnts.size();

						}
						if (!((*it)->getClassType() == "empty")){
							//CEmpty *empty = dynamic_cast<CEmpty*>(*it);
							(*it)->print();
						}

					} catch (...) {

					}
				}
			}
			// cout << col->getPheromone();
			col = col->getEast();
		}
		row = col = row->getSouth();
		cout << "\n";
	}
	cout << "Simulation step number: " << this->turncnt << "\n";
	cout << "Food in Anthill:        " << anthillFood << "\n";
	cout << "Ants born:              " << anthillBornAnts << "\n";
	cout << "Ants back:              " << anthillBackAnts << "\n";
	cout << "Ants died:              " << anthillDiedAnts << "\n";
	cout << "\n";
	cout.flush();
}


void CEnvironment::actAll() {
	this->playground->actAll();
}

CEnvironment::CEnvironment(int M, int N) {

	this->M = M;
	this->N = N;

	/* initialize the playground */
	this->playground = new CAreaList(M, N);

	this->turncnt = 1;

}

CEnvironment::~CEnvironment() {

}

